package coreEngine;

import java.util.Timer;
import java.util.TimerTask;

import main.SatelliteTracker;

public class Chronometer {

	private SatelliteTracker st;

	static int interval;
	static Timer timer;
	private static int hour;
	private static int min;
	private static int sec = 0;
	private static String correctionHour = "";
	private static String correctionMinute = "";
	private static String correctionSecond = "";

	public Chronometer(final SatelliteTracker st, final int interval){
		this.st = st;
		this.interval = interval;

		int delay = 1000;
		int period = 1000;
		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				
				st.updateChronometer(getTimeLeft());
				setInterval();
			}
		}, delay, period);
	}
	private static final int setInterval() {
		//System.out.println(interval);
		if(interval == 0) 
			return interval;
		else 
			return interval--;
	}

	public static String getTimeLeft(){
		if(interval >=3600){
			hour = interval/3600;
			min = (interval%3600)/60;
			sec = (interval%3600)-(min*60);
			if(hour < 10) correctionHour ="0";
			else correctionHour ="";
			if(min < 10) correctionMinute="0";
			else correctionMinute="";
			if(sec < 10) correctionSecond="0";
			else correctionSecond="";
			return correctionHour + hour + ":" + correctionMinute + min + ":" + correctionSecond + sec;
		}
		else{
			if(interval < 3600 && interval >= 60){
				min = (interval%3600)/60;
				sec = (interval%3600)-(min*60);
				if(min < 10) correctionMinute="0";
				else correctionMinute="";
				if(sec < 10) correctionSecond="0";
				else correctionSecond="";
				return "00:" + correctionMinute + min + ":" + correctionSecond + sec;
			}else{
				if(interval == 0) return "00:00:00";
				else{
					sec = (interval%3600)-(min*60);
					if(sec < 10) correctionSecond="0";
					else if(sec == 0) correctionSecond="00";
					else correctionSecond="";
					return "00:00:" + correctionSecond + sec;
				}
			}
		}
	}


	//public void setTimer(int time){
	//	interval = time;
	//}
}
